﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.BL;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Utilities;

namespace Project.Module.DAO.CMSBL
{
    public class CMSEventBL : IBLTable<New, int>
    {
        public void AddEvent(string title, string Description, string ImagePath)
        {
            DateTime date = DateTime.Now;
            title = StringUtil.ISO8859ToUnicode(title);
            content = StringUtil.ISO8859ToUnicode(ImagePath);

            int check = 0;
            if (status == "true")
            {
                check = 1;
            }

            string sql = @"insert into Event (
                              title, content, date, status
                           ) values (
                              {0}, {1}, {2}, {3}
                           )";
            var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { title, content, date, check }).ToList();
        }

        public List<EventModel> GetList()
        {
            var query = from e in this.Context.Event
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.Take(50).ToList();
        }

        public void DeleteEvent(int id)
        {
            New Event = this.Context.Event.Single(c => c.id == id);
            this.Context.Event.DeleteOnSubmit(Event);
            this.Context.SubmitChanges();
        }

        public void EditEvent(int id, string title, string content, string status)
        {
            DateTime date = DateTime.Now;
            int check = 0;
            if (status == "true")
            {
                check = 1;
            }
            title = StringUtil.ISO8859ToUnicode(title);
            content = StringUtil.ISO8859ToUnicode(content);

            string sql = @"update Event 
                           set title = {0}, content = {1}, 
                               status = {2}, date = {3}
                           where id = {4}";
            var query = this.Context.ExecuteQuery<EventModel>(sql, new object[] { title, content, check, date, id }).ToList();
        }

        public EventModel GetDetail(int id)
        {
            var query = from e in this.Context.Event
                        where e.id == id
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.ToList().FirstOrDefault();
        }

        public EventModel GetDetail()
        {
            var query = from e in this.Context.Event
                        where e.status == 1
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.ToList().FirstOrDefault();
        }

        public List<EventModel> Search(string title, string content)
        {
            var query = from e in this.Context.Event
                        where (title != String.Empty && e.title.Contains(title) == true) ||
                        (content != String.Empty && e.content.Contains(content) == true)
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.ToList();
        }

        public List<EventModel> SearchEvent(string title)
        {
            var query = from e in this.Context.Event
                        where e.title.Contains(title) == true
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.ToList();
        }

        public List<EventModel> SearchEventByContent(string content)
        {
            var query = from e in this.Context.Event
                        where e.content.Contains(content) == true
                        orderby e.date descending
                        select new EventModel()
                        {
                            id = e.id,
                            title = e.title,
                            content = e.content,
                            date = e.date != null ? e.date.Value : Convert.ToDateTime("01/01/1900"),
                            status = e.status != null ? e.status.Value : 0,
                        };

            return query.ToList();
        }
    }
}
